package com.df.springmvc.system.manage.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thirdpart.sms.SMSUtil;

import com.common.util.security.EncrypMD5;
import com.df.springmvc.system.log.services.LogServices;
import com.df.springmvc.system.manage.dao.LoginDao;


/**
 * 本类只做数据运算处理
 * @author David
 */
@Service
public class LoginServices{
	@Autowired
	LoginDao dao;
	
	/**
	 * @param args
	 */
	public static void main(String[] args){
		// TODO Auto-generated method stub
	}
	/**
	 * 登录验证
	 * @param u登录名
	 * @param p密码
	 * @return
	 * @throws Exception
	 */
	public ArrayList<String> vali2(String u,String p) throws Exception{
		// TODO Auto-generated method stub
		//ArrayList<String> list=dao.valiUser2(u,p);
		
		return dao.valiUser2(u,EncrypMD5.encode(p));
	}
	/**
	 * 修改密码
	 * @param paramMap
	 * @return
	 * @throws Exception 
	 */
	public Object retrievePassword(HashMap<String, String> map) throws Exception {
		// TODO Auto-generated method stub
		HashMap<String,Object> updmap=new HashMap<String,Object>();
		updmap.put("updateby",map.get("userAccount"));
		updmap.put("updatetime",new Date());
		updmap.put("userAccount", map.get("userAccount"));
		updmap.put("password",EncrypMD5.encode(map.get("password")));//md5密码加密
		Object o=dao.retrievePassword(updmap);
		if("1".equals(o)){
			return "修改成功！";
		}else{
			throw new Exception("修改失败！");
		}
	}
	/**
	 * 发送手机随机验证码
	 * @param paramMap
	 * @return
	 */
	public HashMap<String,String> sendSMS(HashMap<String, String> paramMap) {
		// TODO Auto-generated method stub
		HashMap<String,String> returnmap=new HashMap<String,String>();
		boolean v = dao.queryVerify(paramMap);
		//先检查供应商联系人账号和手机号码时候存在
		if(v){
			SMSUtil smsUtil=new SMSUtil();
			HashMap<String,String> map=new HashMap<String,String>();
			//调用生成随机6位验证码
			String code=getStringRandom(6);
			map.put("code",code);
			String rs=smsUtil.send(paramMap.get("telphone"),"vali",map);
			
			System.out.println("短信接口返回消息"+rs);
			returnmap.put(paramMap.get("telphone"), code);
			//通过短信返回字符串是否包含"true###"来判断是否发送成功
			if(rs.indexOf("true###")!=-1){
				returnmap.put("result", "发送成功！");

			  }else{
				  returnmap.put("result", "发送失败！");
			  }
			//returnmap.put("result", "发送成功！");
			System.out.println(paramMap.get("telphone")+"-----------------------"+code);
		}else{
			returnmap.put("result", "用户账号和手机号码不匹配！！！");
		}
		return returnmap;
	}
	/**
	 * 生成随机数字和字母
	 * @param length 长度
	 * @return
	 */
    public String getStringRandom(int length) {
        String val = "";  
        Random random = new Random();  
        //参数length，表示生成几位随机数  
        for(int i = 0; i < length; i++) {  
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";  
            //输出字母还是数字  
            if( "char".equalsIgnoreCase(charOrNum) ) {  
                //输出是大写字母还是小写字母  
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;  
                val += (char)(random.nextInt(26) + temp);  
            } else if( "num".equalsIgnoreCase(charOrNum) ) {  
                val += String.valueOf(random.nextInt(10));  
            }  
        }  
        return val;  
    }
}
